#pragma once
#include<iostream>
#include<string>
#include<sys/types.h>
#include<sys/time.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/stat.h>
#include<cstdio>
#include<cstring>
#include<fcntl.h>
#include<signal.h>
#include<pthread.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<vector>
#include<arpa/inet.h>
#include<sys/wait.h>
#include<functional>
#include <semaphore.h>
#include<jsoncpp/json/json.h>
#include<sys/select.h>
#include<signal.h>
#include"TcpServer.hpp"
#include"Protocol.hpp"
#include"Log.hpp"
#include"Task.hpp"
#include"ThreadPool.hpp"
#define PORT 8081
using namespace std;
class HttpServer
{
public:
    HttpServer(int port=PORT)
    :_port(port)
    {
    }
    void InitServer()
    {
        //对信号SIGPIPE进行忽略，如果不忽略在写入时候可能直接崩溃server
        signal(SIGPIPE,SIG_IGN);
        tcpserver=TcpServer::getserver(_port);

    }
    void Loop()
    {
        Log(INFO,"Loop begin");
        _listensock=tcpserver->Sock();
        ThreadPool* tp=ThreadPool::get_instance();
        while(!stop)
        {
            struct sockaddr_in peer;
            socklen_t len=sizeof peer;
            int sock=accept(_listensock,(struct sockaddr*)&peer,&len);
            Task task(sock);
            tp->PushTask(task);
        }
    }
private:
  TcpServer* tcpserver;
  bool stop=false;
  int _listensock;
  int _port;
};
